package com.becca.cake.service.system.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.becca.cake.core.constant.CommonConstants;
import com.becca.cake.dao.system.FunctionDAO;
import com.becca.cake.model.system.AdminDO;
import com.becca.cake.model.system.MenuTree;
import com.becca.cake.model.system.Node;
import com.becca.cake.service.system.IUserLoadMenuService;

/**
 * 主页面加载Service实现类
 * @author hejinguo
 * @version $Id: UserLoginMenuServiceImpl.java, v 0.1 2013-12-2 下午01:16:09
 */
@Service
public class UserLoginMenuServiceImpl implements IUserLoadMenuService {
    @Autowired
    private FunctionDAO functionDAO;

    @Override
    public String selectTopMenu(Map<String, Object> map) throws Exception {
        //员工信息
        AdminDO priAdminDO = (AdminDO) map.get("priAdminDO");
        //查询顶部菜单功能
        List<Node> listNode = this.functionDAO.selectTopMenu(map);
        //查询该员工对应的按钮权限
        List<String> buttonList = this.functionDAO.selectButtonFunByAdminId(priAdminDO.getId());
        //查询该员工对应的岗位
        String posts = this.functionDAO.selectAdminPosts(priAdminDO.getId());
        return returnTopMenu(posts, listNode, buttonList, priAdminDO);
    }

    @Override
    public String selectLeftMenu(Map<String, Object> map) throws Exception {
        //根据员工权限获取员工功能
        List<Node> functions = this.functionDAO.selectLeftMenu(map);
        //根据ID查询当前功能
        Node node = this.functionDAO.selectFunctionById(Long.valueOf(map.get("id").toString()));
        //封装菜单树json
        MenuTree tree = new MenuTree();
        tree.getTree(functions, node, false);
        return tree.modifyStr(tree.returnStr.toString());
    }

    /**
     * 返回顶部菜单json
     * @author hejinguo
     * @param posts
     * @param listNode
     * @param buttonList
     * @param priAdminDO
     * @return
     */
    public String returnTopMenu(String posts, List<Node> listNode, List<String> buttonList,
                                AdminDO priAdminDO) {
        StringBuffer msg = new StringBuffer();
        msg.append("{success:true");
        msg.append(",userId:'" + priAdminDO.getId() + "'");
        msg.append(",userName:'" + priAdminDO.getTrueName() + "'");
        msg.append(",postId:'" + posts + "'");
        msg.append(",userSubscriber:'" + priAdminDO.getUserSubIdStr() + "'");
        msg.append(",isSubscription:'" + priAdminDO.getIsSubscription() + "'");
        msg.append(",deptId:'" + priAdminDO.getDeptId() + "'");
        msg.append(",socketUrl:'" + CommonConstants.NODE_SOCKET_URL + "'");
        msg.append(",rights:" + buttonList.toString().replace("[", "'").replace("]", "'"));
        msg.append(",menu:{list:[");
        for (int i = 0; i < listNode.size(); i++) {
            Node priFunctions = listNode.get(i);
            msg.append("{id:'" + priFunctions.getId() + "',");
            msg.append("parentId:'" + priFunctions.getParentId() + "',");
            msg.append("title:'" + priFunctions.getName() + "'}");
            if (i < listNode.size() - 1)
                msg.append(",");
        }
        msg.append("]}}");
        return msg.toString();
    }
}
